

package com.hazelcast.test.starter.constructor;

import com.hazelcast.test.starter.HazelcastStarterConstructor;

import java.lang.reflect.Method;

/**
 * Clones the configuration from {@code mainConfig} to a new configuration object loaded in the
 * target {@code classloader}. The returned configuration has its classloader set to the target classloader.
 */
@HazelcastStarterConstructor(classNames = {"com.hazelcast.config.Config", "com.hazelcast.client.config.ClientConfig"})
public class ConfigConstructor extends AbstractConfigConstructor {

    public ConfigConstructor(Class<?> targetClass) {
        super(targetClass);
    }

    @Override
    Object createNew0(Object delegate) throws Exception {
        ClassLoader classloader = targetClass.getClassLoader();
        Object otherConfig = cloneConfig(delegate, classloader);

        Method setClassLoaderMethod = targetClass.getMethod("setClassLoader", ClassLoader.class);
        setClassLoaderMethod.invoke(otherConfig, classloader);
        return otherConfig;
    }
}
